//
//  MovieTableViewController.m
//  豆瓣_精简版
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieTableViewController.h"
#import "MovieTableViewCell.h"
#import "AppTools.h"
#import "Movie.h"
#import "MJRefresh.h"
#import "MovieDetailViewController.h"
#import "DataBase.h"
@interface MovieTableViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating>

@property(nonatomic,retain)UITableView *myTableView;
@property (nonatomic, assign)BOOL isUpLoad;
@property(nonatomic,retain)UISearchController *searcherC;

@end

@implementation MovieTableViewController

- (void)dealloc
{
    [_myTableView release];
    [_movieArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];
    self.searcherC = [[UISearchController alloc]initWithSearchResultsController:nil];
    self.searcherC.searchResultsUpdater = self;
    //    设置开始搜索时背景显示与否 页面背景阴影效果,一般不要设置.
    self.searcherC.dimsBackgroundDuringPresentation = NO;
    //设置点击搜索框时候隐藏导航栏
    self.searcherC.hidesNavigationBarDuringPresentation = NO;
    self.searcherC.searchBar.frame = CGRectMake(self.searcherC.searchBar.frame.origin.x, self.searcherC.searchBar.frame.origin.y, self.searcherC.searchBar.frame.size.width, 44.0);
    
    //    [searchBar sizeToFit]:设置searchBar位置自适应
    [self.searcherC.searchBar sizeToFit];
    
    
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
//    [self.myTableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.myTableView.tableHeaderView = self.searcherC.searchBar;
    [self.myTableView release];
    
    
    
    self.searchArray = [NSMutableArray array];
    self.movieArray = [NSMutableArray array];

    
    
    [self addHeaderRefresh];
    [self addFooterRefresh];
    
    self.isUpLoad = NO; // 默认不是上拉
    
    
    [[DataBase shareDataBase]openDB];
    self.movieArray = [[DataBase shareDataBase]selectMovieDetailData];
    
    
    
    
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
    NSString *searchText = [self.searcherC.searchBar text];
    [self.searchArray removeAllObjects];
    for (Movie *movie in self.movieArray) {
        
        if ([movie.movieName rangeOfString:searchText].location != NSNotFound) {
            [self.searchArray addObject:movie];
        }
    }
    [self.myTableView reloadData];
    
}


//设置下拉刷新
-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    [self.myTableView addHeaderWithCallback:^{
        
        NSLog(@"下拉");
        self.isUpLoad = NO;
        // 重新请求数据
        [self getData];
    }];
    // 进到应用自动刷新 放到ViewDidLoad也一样
    [self.myTableView headerBeginRefreshing];
}

//上啦加载
-(void)addFooterRefresh
{
    //当上拉时候触发该方法
    [self.myTableView addFooterWithCallback:^{
        
        NSLog(@"上拉");
        self.isUpLoad = YES;
        [self getData];
    }];
}

//获取网络数据
- (void)getData{
    
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSString *huancun = @"movieHuanCun";
        [[DataBase shareDataBase]dropTable:huancun];
        [[DataBase shareDataBase]createHuanCunTable];
        
        if (self.isUpLoad == NO) {
            
            // 清空数组 上拉和下拉做判断 YES NO
            [self.movieArray removeAllObjects];
            NSLog(@"判断是下拉,清空数组");
        }
        else
        {
            NSLog(@"不用清空");
        }
        
        if (result == nil) {
            NSLog(@"===%@",result);
            return;
        }
        
        NSMutableDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"result"];
        for (NSMutableDictionary *dic in array) {
            //把数据存入模型,再存入数组
            Movie *movie = [[Movie alloc]init];
            [movie setValuesForKeysWithDictionary:dic];
            [self.movieArray addObject:movie];
            
            [[DataBase shareDataBase]insertMovieDetailData:movie];
        }
        
        //结束下拉上拉刷新
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        
        [self.myTableView reloadData];
        
    }];
    [self.myTableView footerEndRefreshing];
}



//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.searcherC.active) {
        return self.searchArray.count;
    }
    else {
        return self.movieArray.count;
    }
    
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        
        cell = [[MovieTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    
    if (self.searcherC.active) {
        Movie *movie = [self.searchArray objectAtIndex:indexPath.row];
        cell.myIndexPath = indexPath;
        cell.movie = movie;
    }
    else {
        Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
        cell.myIndexPath = indexPath;
        cell.movie = movie;
    }
    
    
//    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
//    cell.movie = movie;
    
    return cell;
}

//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    Movie *movie = nil;
    
    if (self.searcherC.active) {
        
        NSLog(@"选中搜索列表");
        movie = [self.searchArray objectAtIndex:indexPath.row];
        
    }
    else {
        
        NSLog(@"选中所有列表");
        movie = [self.movieArray objectAtIndex:indexPath.row];
        
    }
    
    
    
    
    MovieDetailViewController *movieDVC = [[MovieDetailViewController alloc]init];
    [self.navigationController pushViewController:movieDVC animated:YES];
    movieDVC.movie = movie;
    
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.searcherC.active) {
        self.searcherC.active = NO;
        [self.searcherC.searchBar removeFromSuperview];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
